package pl.edu.pw.ee.buildcity.ui.web.jsf.converters;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Configurable;
import pl.edu.pw.ee.buildcity.entities.BuildDefinition;
import pl.edu.pw.ee.buildcity.services.BuildDefinitionService;
import pl.edu.pw.ee.buildcity.ui.web.jsf.utils.JSFUtils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 * @author mgrzechocinski
 * @since 2009-11-15
 */
@Configurable
public class BuildDefinitionConverter implements Converter {

	private static final Logger log = Logger.getLogger(BuildDefinitionConverter.class);

	private final BuildDefinitionService buildDefinitionService = (BuildDefinitionService) JSFUtils.getSpringBean("buildDefinitionService");

	@Override
	public Object getAsObject(FacesContext fc, UIComponent component, String buildDefName) {
		BuildDefinition foundByName = null;
		if (buildDefName != null) {
			foundByName = buildDefinitionService.getByName(buildDefName);
		}
		return foundByName;
	}

	@Override
	public String getAsString(FacesContext fc, UIComponent component, Object objectToConvert) {
		String asString = null;
		if (objectToConvert != null) {
			asString = ((BuildDefinition) objectToConvert).getName();
		}
		return asString;
	}
}
